#include <iostream>
#include <fstream>
#include <numeric>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#include <string.h>
#include <typeinfo>

#include "datatype.h"
#include "server.h"

server::server(const char *ip,const char *port) : client(ip,port)
{
	if(bind(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0)
	{
		errlog("bind");
	}

	myDs.open("clients.db");
}
server::~server() 
{

}
int server::regist(void)
{
	myDs.insert(r_message.name,r_message.text);
	login();
}
int server::login(void)
{
	if(myDs.find(r_message.name,r_message.text) > 0)
	{
		clients.push_back(clientaddr);
		cout << "new login ,server num: " << clients.size() << endl;
	}
	else
	{
		cout << "user is not found login fail" << endl;
	}
}
int server::logout(void)
{
	for(client_itor = clients.begin();client_itor != clients.end();client_itor++)
	{
		if(memcmp(&(*client_itor),&clientaddr,sizeof(clientaddr)) == 0)
		{
			cout << "find offline client " << " " << ntohs(clientaddr.sin_port) << " " ;
			client_itor = clients.erase(client_itor);
		}
	}
	cout << r_message.name  << " is offline" << endl;
}
bool server::user_check_exist(struct sockaddr_in user)
{
	bool user_exist_flag = false;
	for(client_itor = clients.begin();client_itor != clients.end();client_itor++)
	{
		if(memcmp(&(*client_itor),&clientaddr,sizeof(clientaddr)) == 0)
		{
			user_exist_flag = true;
		}
	}
	return user_exist_flag;

}
int server::message(void)
{
	cout << "receive from " << r_message.name << ": " << r_message.text << endl;
	if(!clients.empty())
	{
		for(client_itor = clients.begin();client_itor != clients.end();client_itor++)
		{
			s_message = r_message;

			if((memcmp(&(*client_itor),&clientaddr,sizeof(clientaddr)) != 0) &&		\
					(memcmp(&(*client_itor),&serveraddr,sizeof(serveraddr)) != 0))
			{
				my_send(*client_itor);
			}
		}
	}
	return 0;
}

int server::get_time(string &_sTime)
{
	time_t t;
	struct tm *tp;

	time(&t);
	tp = localtime(&t);

	stringstream ssTime;

	ssTime << tp->tm_year + 1900;
	ssTime << "/" ;
	ssTime << tp->tm_mon + 1;
	ssTime << "/" ;
	ssTime << tp->tm_mday;
	ssTime << " " ;
	ssTime << tp->tm_hour ;
	ssTime << ":" ;
	ssTime << tp->tm_min;
	ssTime << ":" ;
	ssTime << tp->tm_sec;
	ssTime << " " ;

	_sTime.assign(ssTime.str());
}
int server::fs_write_message(struct message_t message)
{
	string message_record;
	string sTime;

	message_record = "";
	message_record.append(message.name);
	message_record.append(" : ");
	message_record.append(message.text);

	ofstream fs("data.txt",ios::app);

	get_time(sTime);

	fs << sTime << " " << message_record << endl;

	cout << "receive name is -------> " << message.name << endl;
	fs.close();
}
//注意：父子进程数据不能共用
int server::child_process()
{
	while(1)
	{
		s_message.type = MSG_TYPE_MESSAGE;
		strcpy(s_message.name,"server");
		cin.getline(s_message.text,MESSAGE_TEXT_SIZE);
		my_send(serveraddr);
	}
}

int server::parent_process()
{

	while(1)
	{
		my_receive(clientaddr);
		cout << "--------------------receive------------------------" << endl;
		if(r_message.type == MSG_TYPE_REGIST)
		{
			regist();
		}
		else if(r_message.type == MSG_TYPE_LOGIN)
		{
			login();
		}
		else if(r_message.type == MSG_TYPE_LOGOUT)
		{
			if(!user_check_exist(clientaddr))
			{
				continue;
			}
			logout();
		}
		else if(r_message.type == MSG_TYPE_MESSAGE)
		{
			if(!user_check_exist(clientaddr))
			{
				continue;
			}
			fs_write_message(r_message);
			message();
		}
	}
}
